package chapter7.exercise;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
	static Vehicle vehicle=null;
	static Scanner scanner=new Scanner(System.in);
	static int type;
	static void showMenu(){
		System.out.println("请输入车辆种类");
		System.out.println("1.小汽车");
		System.out.println("2.大巴");
		System.out.println("3.卡车");
		System.out.println("0.退出");
		System.out.println("其他功能正在开发中");
	}
	static void getType(){
		while(true){
			try{
				showMenu();
				type=scanner.nextInt();
				switch (type){
					case 1:vehicle=new Car();break;
					case 2:vehicle=new Bus();break;
					case 3:vehicle=new Truck();break;
					case 0:
						System.out.println("欢迎下次使用");
						System.exit(0);
					default:
						if(type<0) System.out.println("输入无效");
						else System.out.println("请等待新车型录入系统");
						continue;
				}
				vehicle.end();
				System.gc();
			}
			catch (InputMismatchException ime){
				System.out.println("请输入数字！");
			}
		}
	}
	public static void main(String[] args) {
		while (true){
			getType();
		}
	}

}
abstract class Vehicle{
	String plate;
	String type;
	double mile;
	Vehicle(String type){
		Scanner scanner=new Scanner(System.in);
		this.type=type;

		System.out.println("请输入车牌号");
		this.plate =scanner.next();

		System.out.println("正在检查是否违章");
		System.out.println("无违章记录");

		while (true){
			try {
				System.out.println("请输入里程数");
				this.mile=scanner.nextDouble();
				break;
			}
			catch (InputMismatchException ime){
				System.out.println("请输入数字！");
			}
		}

	}
	double charge(){
		return this.mile;
	}
	void end(){
		Scanner scanner=new Scanner(System.in);
		String confirm;


		System.out.println("车辆信息：");
		System.out.println("\t车辆种类：\t"+this.type);
		System.out.println("\t车牌号：\t"+this.plate);
		System.out.println("\t里程数（公里）：\t"+this.mile);
		System.out.println("\t应缴费（元）：\t"+this.charge());

		while (true){
			System.out.println("请输入1确认");
			confirm=scanner.next();
			if(confirm.equals("1")){
				System.out.println("付费成功，谢谢，祝您一路顺风");
				break;
			}
			else{
				System.out.println("信息确认失败，请再试一次");
			}
		}
	}
}
class Car extends Vehicle{
	private final double chargePerKM=0.5;
	Car(){
		super("小汽车");
	}
	@Override
	double charge() {
		return super.charge()*this.chargePerKM;
	}
}
class Bus extends Vehicle{
	private final double chargePerKM=1;
	Bus(){
		super("大巴车");
	}
	@Override
	double charge() {
		return super.charge()*this.chargePerKM;
	}
}
class Truck extends Vehicle{
	private double weight;
	private final double chargePerKmKg=5.0;
	Truck(){
		super("卡车");
		Scanner scanner=new Scanner(System.in);
		while (true){
			try {
				System.out.println("请输入车重");
				this.weight=scanner.nextDouble();
				break;
			}
			catch (InputMismatchException ime){
				System.out.println("请输入数字!");
			}
		}
	}
	@Override
	double charge() {
		return super.charge()*this.weight*this.chargePerKmKg;
	}
}

